package com.home.duy.code.components;

import java.awt.TextField ;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter ;
import java.awt.event.KeyEvent ;

import javax.swing.JTextField;

import com.home.Validation.Validator;
 
public class NumbericTextField extends JTextField
{
 public NumbericTextField (String _initialStr, int _col)
 { 
    super (_initialStr, _col) ;
    this.setHorizontalAlignment(JTextField.RIGHT);   
    this.addKeyListener(new KeyAdapter()
   {
       public void keyTyped (KeyEvent e) 
        { 
            char c = e.getKeyChar() ;
                
            if (!   ((c==KeyEvent.VK_BACK_SPACE) || (c==KeyEvent.VK_DELETE) 
                ||  (c== KeyEvent.VK_ENTER)      || (c == KeyEvent.VK_TAB) 
                ||  (Character.isDigit(c)))) 
            {
               e.consume() ;
           }
           
        } 
    });
    
    this.addFocusListener(new FocusListener() {
		
		@Override
		public void focusLost(FocusEvent arg0) {
			if(Validator.isBlank(getText()))
			setText("0");
		}
		
		@Override
		public void focusGained(FocusEvent arg0) {
			setText("");
		}
	});
 } 
        
  public NumbericTextField (int _col) 
  { 
    this ("", _col) ; 
  } 
}